"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 系统内部使用工具
Case Name   : pscp指定-e并指定不存在的错误信息输出目录
Create At   : 2022/4/24
@zou_jialiang0501129488
Description :
    1.构建传输源文件
    2.pscp指定-e并指不存在的错误信息输出文件
    3.清理环境
Expect      :
    1.构建成功
    2.执行成功，错误信息存储于错误信息输出文件中
    3.清理成功
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.Common import CommonSH
from testcase.utils.Logger import Logger

primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 > primary_sh.get_node_num(), '非1+2不执行')
class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_root_node = Node('PrimaryRoot')
        self.primary_node = Node('PrimaryDbUser')
        self.s_root_node1 = Node('Standby1Root')
        self.s_root_node2 = Node('Standby1Root')
        self.ssh_file = os.path.join('~', '.ssh', '*')
        self.s_node1 = Node("Standby1DbUser")
        self.s_node2 = Node("Standby2DbUser")
        self.com_root = CommonSH("PrimaryRoot")
        self.common = Common()
        self.dir_path = os.path.join(os.path.dirname(macro.DB_INSTANCE_PATH),
                                     'err_path')

    def test_pscp(self):
        text = '-----step1:pscp指定-e并指定不存在的错误信息输出文件;expect:成功-----'
        self.log.info(text)
        self.log.info("-----创建root互信-----")
        result = self.common.get_sh_result(self.primary_root_node,
                                           f"ls {macro.DB_SCRIPT_PATH}")
        if "gs_sshexkey" not in result:
            cmd = f"cd {macro.DB_SCRIPT_PATH}/../; " \
                  f"tar -zxvf openGauss-Package-bak*.tar.gz > /dev/null"
            result1 = self.common.get_sh_result(self.primary_root_node, cmd)
            self.log.info(result1)
            result2 = self.common.get_sh_result(self.primary_root_node,
                                                f"ls {macro.DB_SCRIPT_PATH}")
            self.log.info(result2)
            if "gs_sshexkey" not in result2:
                raise Exception("cat not find gs_sshexkey, Please check!")

        self.host_tuple = (self.primary_root_node.ssh_host,
                           self.s_node1.ssh_host, self.s_node2.ssh_host)
        self.params = {'-f': 'test_hosts'}
        self.com_root.exec_gs_sshexkey(macro.DB_SCRIPT_PATH,
                                       *self.host_tuple,
                                       **self.params)
        test = '-----查看主机名-----'
        self.log.info(test)
        node_name = self.common.get_sh_result(self.s_root_node1, "uname -n")
        self.assertIsNotNone(node_name, '执行失败:' + text)

        text = '-----step2:pscp指定-e并指不存在的错误信息输出文件;expect:成功-----'
        self.log.info(text)
        self.log.info('-----执行pscp命令-----')
        file_path = os.path.join(f'{self.dir_path}', 'not_exists_file')
        pscp_cmd = f"pscp -s -e {self.dir_path} -H {node_name} " \
                   f"{file_path} {self.dir_path}"
        self.log.info(pscp_cmd)
        pscp_res = self.common.get_sh_result(self.primary_root_node, pscp_cmd)
        self.assertIn('No such file or directory', pscp_res, '执行失败')
        global err_path
        err_path = os.path.join(self.dir_path, f'{node_name}')
        check_cmd = f"cat {err_path}"
        self.log.info(check_cmd)
        check_res = self.common.get_sh_result(self.primary_root_node, check_cmd)
        self.assertIn('No such file or directory', check_res, '执行失败')

    def tearDown(self):
        text = f'-----step3:清理环境;expect:成功-----'
        self.log.info(text)
        del_res = self.common.get_sh_result(self.primary_root_node,
                                            f'rm -rf {self.dir_path}')
        self.log.info('-----删除root互信-----')
        del_cmd = f'rm -rf {self.ssh_file}'
        self.log.info(del_cmd)
        del_res1 = self.common.get_sh_result(self.primary_root_node, del_cmd)
        del_res2 = self.common.get_sh_result(self.s_root_node1, del_cmd)
        del_res3 = self.common.get_sh_result(self.s_root_node2, del_cmd)
        self.assertEqual('', del_res1, '互信删除失败')
        self.assertEqual('', del_res2, '互信删除失败')
        self.assertEqual('', del_res3, '互信删除失败')
        self.assertEqual('', del_res, '文件删除失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
